using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class LoaiKhachHangRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //

		                                                          

		//////////////////////////////////////////////////////////

        public static LoaiKhachHangDTO CreateLoaiKhachHangDTOFromLoaiKhachHangEF(LoaiKhachHang objEf) // EF -> DTO
        {
            LoaiKhachHangDTO objDTO = new LoaiKhachHangDTO();
            Mapper.CreateMap<LoaiKhachHang, LoaiKhachHangDTO>();
            objDTO = Mapper.Map<LoaiKhachHang, LoaiKhachHangDTO>(objEf);
            return objDTO;
        }

        public static LoaiKhachHang CreateLoaiKhachHangEFFromLoaiKhachHangDTO(LoaiKhachHangDTO objDTO)    //DTO -> EF
        {
            LoaiKhachHang objEF = new LoaiKhachHang();
            Mapper.CreateMap<LoaiKhachHangDTO, LoaiKhachHang>();
            objEF = Mapper.Map<LoaiKhachHangDTO, LoaiKhachHang>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.LoaiKhachHangList
                   select x.MaLoaiKhachHang).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.LoaiKhachHangList
                        orderby x.MaLoaiKhachHang descending
                        select x.MaLoaiKhachHang;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(LoaiKhachHangDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                LoaiKhachHang objEF = CreateLoaiKhachHangEFFromLoaiKhachHangDTO(objDTO);
                context.AddToLoaiKhachHangList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(LoaiKhachHangDTO objDTO)
        {
            try
            {
                LoaiKhachHang objEF = CreateLoaiKhachHangEFFromLoaiKhachHangDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.LoaiKhachHangList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<LoaiKhachHangDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.LoaiKhachHangList
                        orderby x.MaLoaiKhachHang
                        select x).Skip(Start - 1).Take(Record);
            List<LoaiKhachHangDTO> lst = new List<LoaiKhachHangDTO>();
            foreach (var obj in query)
            {
                LoaiKhachHangDTO lp = LoaiKhachHangRepository.CreateLoaiKhachHangDTOFromLoaiKhachHangEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<LoaiKhachHangDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<LoaiKhachHangDTO> lst = new List<LoaiKhachHangDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from LoaiKhachHang in context.LoaiKhachHangList
                        select LoaiKhachHang;
            foreach (var objEf in query)
            {
                LoaiKhachHangDTO objDTO = CreateLoaiKhachHangDTOFromLoaiKhachHangEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public LoaiKhachHangDTO LayTheoMa(int MaLoaiKhachHang)
        {
            LoaiKhachHangDTO objDTO = new LoaiKhachHangDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from LoaiKhachHang in context.LoaiKhachHangList
                            where LoaiKhachHang.MaLoaiKhachHang == MaLoaiKhachHang
                            select LoaiKhachHang;
                LoaiKhachHang objEF = query.First();
                objDTO = CreateLoaiKhachHangDTOFromLoaiKhachHangEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }
        public bool Xoa(int MaLoaiKhachHang, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                       KhachHangRepository KhachHangRepo = new KhachHangRepository();
                       KhachHangRepo.XoaTheoMaLoaiKhachHang(MaLoaiKhachHang, XoaKhoaNgoai);

                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.LoaiKhachHangList
                            where x.MaLoaiKhachHang == MaLoaiKhachHang 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

